package com.yuxiu.edu.web.controller;

import com.yuxiu.edu.model.Employee;
import com.yuxiu.edu.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Resource
    @Autowired
    private IEmployeeService employeeService;

//    @RequestMapping("find")
//    public String find(Integer id){
//        System.out.println(".....");
//        Employee employee = employeeService.findById(id);
//        System.out.println(employee);
//        return null;
//    }
    @RequestMapping("select")
    public String select(String text,Model model){
        System.out.println("==================================================");
        //数据库查询username对应password
        if(text !=null) {
            List<Employee> employeeList = employeeService.findByText(text);
            System.out.println(employeeList);
            System.out.println("==================================================");
            if (employeeList != null) {
                model.addAttribute("employeeList", employeeList);
                return "Employee/EmployeeInfo";
            }
            else{
                List<Employee> employee= employeeService.findByAll();
                System.out.println(employee);
                //2.存数据
                model.addAttribute("employeeList",employee);
                return "Employee/EmployeeInfo";
            }
        }
        else{
            List<Employee> employeeList= employeeService.findByAll();
            System.out.println(employeeList);
            //2.存数据
            model.addAttribute("employeeList",employeeList);
            return "Employee/EmployeInfo";
        }
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        //保存数据
        System.out.println("=======================删除===========================");
        employeeService.deleteById(id);
        System.out.println("========================删除==========================");
        return "forward:list.do";
    }

    @RequestMapping("update")
    public String update(Integer id,Model model){
        System.out.println("======================修改页面===========================");
        System.out.println("id:" + id);
        System.out.println("=======================修改===========================");
        //通过id找到商品
        Employee employee = employeeService.findById(id);
        System.out.println(employee);
        if (employee != null){
            model.addAttribute("employee",employee);
        }
        return "Employee/EmployeeUpdate";
    }

    @RequestMapping("save")
    public String save(String chineseName, String englishName, Date joinedDate, BigDecimal salary,
                       Integer positionLevel, String departmentId, String deltag, Date leaveDate,Integer id){
        //
        System.out.println("=======================添加开始===========================");
        Employee employeeL  = employeeService.findById(id);
        Employee employee = new Employee();
        employee.setChineseName(chineseName);
        employee.setEnglishName(englishName);
        employee.setJoinedDate(joinedDate);
        employee.setSalary(salary);
        employee.setPositionLevel(positionLevel);
        employee.setDepartmentId(departmentId);
        employee.setDeltag(deltag);
        employee.setLeaveDate(leaveDate);
//        employee.setId(employeeL.getId());
        System.out.println("=======================有无ID===========================");
//        System.out.println(employee.getId());
//        items.setCreatetime(new Date());

        //保存数据
        employeeService.saveOrUpdate(employee);

        return "forward:list.do";
    }

    @RequestMapping("list")
    public String list(Model model){
        //1.查数据
        List<Employee> employeeList= employeeService.findByAll();
        System.out.println(employeeList);
        //2.存数据
        model.addAttribute("employeeList",employeeList);
        return "Employee/EmployeeInfo";
    }

    @RequestMapping("manage")
    public String manage(){
        return "Employee/EmployeeManage";
    }

    @RequestMapping("edit")
    public String edit(){
        return "Employee/EmployeeEdit";
    }

    @RequestMapping("other")
    public String other(){
        return "Employee/EmployeeOther";
    }
}
